package com.white.commonlib.widget;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ytf on 2017/4/10 010.
 * Description:     环形线，中间两行字
 *(fontMetrics.top是个负值，就是text顶部到baseline的距离)
 * 计算rect中text的baseline， rect.centerY - fontMentrics.top - (fm.bottom- fm.top)/2， 再化简
 */

public class V1 extends AbsView
{
    private Paint  mTextPaint;
    private Paint mProgressBgPaint, mProgressPaint;
    private int color = 0xFFFFFFFF;
    private int progressColor = 0xFF45CCD2;
    private int progressBgColor = 0x33FFFFFF;

    private int width, height;
    private float centerTextSize, subtextSize;
    private float lineLength = 0;
    private Rect circleOutRect,circleInnerRect, centerRect, subtextRect;
    RectF progressRect;
    private float radiusOut, radiusIn;
    private List<Line> lineList = new ArrayList<>();

    private ValueAnimator numPulseAnim, rotateProgressAnim;

    private int centerNum = 0;
    private int magicNumAnim = 0;

    private float progress = 0;
    private float magicProgress = 0;

    private String subStr = "总分数 120";

    public V1(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init();
    }

    public V1(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    public int getDefaultWrapWidth()
    {
        return 100;
    }

    @Override
    public int getDefaultWrapHeight()
    {
        return 100;
    }

    private void init()
    {

        mProgressBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mProgressBgPaint.setColor(progressBgColor);
        mProgressBgPaint.setStyle(Paint.Style.STROKE);
        mProgressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mProgressPaint.setColor(progressColor);
        mProgressPaint.setStyle(Paint.Style.STROKE);

        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setColor(color);

        circleOutRect = new Rect();
        circleInnerRect = new Rect();
        progressRect = new RectF();

        centerRect = new Rect();
        subtextRect = new Rect();

    }

    private boolean isAnimGoing;

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        drawCenterText(canvas);
        drawSubtext(canvas);
        drawProgressBg(canvas);
        drawProgressCircle(canvas);
    }

    private void drawProgressCircle(Canvas canvas)
    {
        if(isAnimGoing)
        {
            canvas.drawArc(progressRect, 270, magicProgress,false, mProgressPaint);
        }else
        {
            canvas.drawArc(progressRect, 270, progress,false, mProgressPaint);
        }
    }

    private void drawProgressBg(Canvas canvas)
    {
        canvas.drawCircle(centerX, centerY, progressRect.width()/2, mProgressBgPaint);
    }

    public void animIt()
    {
        if(isAnimGoing || centerNum < 10)
            return;
        numPulseAnim = ValueAnimator.ofInt(0, centerNum);
        numPulseAnim.setDuration(850);
        numPulseAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
        {
            @Override
            public void onAnimationUpdate(ValueAnimator animation)
            {
                isAnimGoing = true;
                magicNumAnim = (int) animation.getAnimatedValue();
                postInvalidate();
//                postInvalidate(centerRect.left, centerRect.top, centerRect.right, centerRect.bottom);
            }
        });

        rotateProgressAnim = ValueAnimator.ofFloat(0, progress);
        rotateProgressAnim.setDuration(850);
        rotateProgressAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
        {
            @Override
            public void onAnimationUpdate(ValueAnimator animation)
            {
                isAnimGoing = true;
                magicProgress = (float) animation.getAnimatedValue();
            }
        });
        AnimListener l = new AnimListener();
        numPulseAnim.addListener(l);
        rotateProgressAnim.addListener(l);

        numPulseAnim.start();
        rotateProgressAnim.start();
    }

    private class AnimListener implements Animator.AnimatorListener
    {

        @Override
        public void onAnimationStart(Animator animation)
        {

        }

        @Override
        public void onAnimationEnd(Animator animation)
        {
            isAnimGoing = false;
        }

        @Override
        public void onAnimationCancel(Animator animation)
        {
            isAnimGoing = false;
        }

        @Override
        public void onAnimationRepeat(Animator animation)
        {
            isAnimGoing = false;
        }
    }

    public void stop()
    {
        magicNumAnim = 0;
        magicProgress = 0;
        if(numPulseAnim != null)
        {
            numPulseAnim.cancel();
        }
        if(rotateProgressAnim != null)
        {
            rotateProgressAnim.cancel();
        }
    }

    private void drawSubtext(Canvas canvas)
    {
        mTextPaint.setTextSize(subtextSize);
        canvas.drawText(subStr, subtextRect.centerX(), subtextRect.bottom, mTextPaint);
    }

    private void drawCenterText(Canvas canvas)
    {
        mTextPaint.setTextSize(centerTextSize);
        if(isAnimGoing)
        {
            String str = String.valueOf(magicNumAnim);
            canvas.drawText(str, centerRect.centerX(), centerRect.bottom, mTextPaint);
        }else
        {
            canvas.drawText(String.valueOf(centerNum), centerRect.centerX(), centerRect.bottom, mTextPaint);
        }
    }

    @Override
    protected void onDetachedFromWindow()
    {
        super.onDetachedFromWindow();
        if(numPulseAnim != null)
        {
            numPulseAnim.cancel();
        }
    }

    @Override
    public void onGlobalLayout()
    {
        if (!isLayout)
        {
            isLayout = true;
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        super.onSizeChanged(w, h, oldw, oldh);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
        initDimension();
    }

    private float centerX, centerY;
    private void initDimension()
    {
        radiusOut = width / 2.0f;
        centerX = radiusOut;
        centerY = height / 2.0f;

        radiusIn = radiusOut - lineLength;
        float padding = radiusIn / 10;
        float progressStrokeWidth = width/20;
        float progressPaddingLine = 0;

        mProgressPaint.setStrokeWidth(progressStrokeWidth);
        mProgressBgPaint.setStrokeWidth(progressStrokeWidth);
        progressRect.left = lineLength + progressPaddingLine + progressStrokeWidth/2;
        progressRect.top = progressRect.left;
        progressRect.right = width - lineLength - progressPaddingLine - progressStrokeWidth/2;
        progressRect.bottom = height - lineLength - progressPaddingLine - progressStrokeWidth/2;

        centerTextSize = radiusIn / 5.0f * 3;
        subtextSize = centerTextSize * 0.4f;

        centerRect.left = 0;
        centerRect.top = (int) (height/2 - centerTextSize);
        centerRect.right = width;
        centerRect.bottom = width/2;

        subtextRect.left = 0;
        subtextRect.top = (int) (width/2 + padding);
        subtextRect.right = width;
        subtextRect.bottom = (int) (subtextRect.top + subtextSize + padding);

        circleOutRect.left = 0;
        circleOutRect.top = 0;
        circleOutRect.right = width;
        circleOutRect.bottom = height;

        circleInnerRect.left = (int) lineLength;
        circleInnerRect.top = (int) lineLength;
        circleInnerRect.right = (int) (circleOutRect.right - lineLength);
        circleInnerRect.bottom = (int) (circleOutRect.bottom - lineLength);

    }


    public void setSubStr(String subStr)
    {
        this.subStr = subStr;
        invalidate();
    }

    public int getCenterStr()
    {
        return centerNum;
    }

    public void setProgress(int ct)
    {
        if(ct > 100)
            ct = 100;
        this.centerNum = ct;
        magicNumAnim = centerNum;
        progress = ct * 3.6f;
        magicProgress = progress;
        invalidate();
    }

    class Line
    {
        float startX,startY, endX, endY;

        public Line(float startX, float startY, float endX, float endY)
        {
            this.startX = startX;
            this.startY = startY;
            this.endX = endX;
            this.endY = endY;
        }
    }

}
